package brian.huang.reader;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.TextView;

import brian.huang.reader.ReaderFragment.ContentReader;
import brian.huang.reader.ReaderFragment.ContentReader.LoadCompletedListener;
import brian.huang.reader.ReaderFragment.ContentReader.Page;

import java.util.ArrayList;

public class InfinitePagerAdapter extends PagerAdapter
        implements LoadCompletedListener {
    private static final String TAG = "InfinitePagerAdapter";
    private Context mContext;
    private ContentReader mReader;
    private ViewPager mViewPager;
    private ArrayList<Page> mPageList;

    public InfinitePagerAdapter(Context context, ContentReader reader, ViewPager pager) {
        mContext = context;
        mReader = reader;
        mViewPager = pager;

        mPageList = new ArrayList<Page>();
    }
    public InfinitePagerAdapter(Context context, ContentReader reader, ViewPager pager, String bookmark) {
    	this(context, reader, pager);

        // Load the first page.
        Page page = mReader.getPage(bookmark);
        mPageList.add(page);
    }

    public InfinitePagerAdapter(Context context, ContentReader reader, ViewPager pager, int chapter) {
        this(context, reader, pager);

        Page page = mReader.getPage(chapter);
        mPageList.add(page);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.i(TAG, "instantiateItem position: " + position);

        Page page = mPageList.get(position);
        page.setLoadCompletedListener(this);
        ((ViewPager) container).addView(page.getView(), 0);
        page.loadPage();

        return page;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Log.i(TAG, "destroyItem(), position=" + position + ", view's position=" + getItemPosition(object));

        Page page = (Page)object;
        page.setLoadCompletedListener(null);
        ((ViewPager) container).removeView(page.getView());
    }

    public void onLoadCompleted(View view, Page page) {
        Log.i(TAG, "onLoadCompleted(), index=" + mPageList.indexOf(page));
        int index = mPageList.indexOf(page);
        boolean isLastOne = index == mPageList.size()-1;
        if (isLastOne && !page.isLast()) {
            Page nextPage = page.getNextPage();
            mPageList.add(nextPage);
        }
    
        boolean isFirstOne = index == 0;
        if (isFirstOne && !page.isFirst()) {
            Page previousPage = page.getPreviousPage();
            mPageList.add(0, previousPage);
        }
    
        notifyDataSetChanged();
    }
    @Override
    public int getItemPosition(Object object) {
        int i = mPageList.indexOf(object);
        Log.i(TAG, "getItemPosition(" + object + "), i=" + i);
        return i;
    }

    @Override
    public int getCount() {
        return mPageList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((Page)object).getView();
    }

    @Override
    public void startUpdate(ViewGroup container) {
        //Log.i(TAG, "     /^ startUpdate()  ^\\");
    }
    @Override
    public void finishUpdate(ViewGroup container) {
        //Log.i(TAG, "     \\_ finishUpdate() _/");
    }
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        //Log.i(TAG, "setPrimaryItem(), position = " + position + ", object's position = " + mPageList.indexOf(object));
    }

    public ArrayList<Page> getPageList() {
        return mPageList;
    }
}
